/**
 * sysUserRoleServiceImpl
 * 项目名称:分秒金融
 *
 * @author lunli
 *
 * 功能描述:用户角色管理
 * 
 * 历史版本:2017-03-28 v1.0.0 lunli  创建:
 * 
 */
package com.fmjr.sys.sysUserRole.service.impl;

import com.fmjr.common.base.exception.BusinessException;
import com.fmjr.common.base.util.Pager;
import com.fmjr.common.util.SqlUtil;
import com.fmjr.sys.sysUserRole.bean.SysUserRole;
import com.fmjr.sys.sysUserRole.dao.SysUserRoleDao;
import com.fmjr.sys.sysUserRole.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("sysUserRoleService")
public class  SysUserRoleServiceImpl implements ISysUserRoleService {
	

	@Autowired
    private SysUserRoleDao sysUserRoleDao;
	@Autowired 
	private ISysUserRoleService sysUserRoleService;
	

	/**
	 * 根据条件查询用户角色记录
	 * @author lunli
	 * @param  pager
	 * @return SysUserRoleList
	 * @throws BusinessException
	 */
	public void selectSysUserRoleList(Pager<SysUserRole> pager) throws BusinessException{
		int totalCount = sysUserRoleDao.selectSysUserRoleCount(pager);
		int numPerPage = SqlUtil.checkPageSize(pager.getNumPerPage());
		int currentPage = SqlUtil.checkPageCurrent(totalCount, numPerPage, pager.getPageNum());
		pager.setLimitStart(SqlUtil.countOffset(currentPage, numPerPage));
		pager.setPageSize(numPerPage);
		pager.setCurrentPage(currentPage);
		pager.setTotalCount(totalCount);
		pager.setTotalPage(totalCount%numPerPage==0?totalCount/numPerPage:totalCount/numPerPage+1);
		List<SysUserRole> list = sysUserRoleDao.selectSysUserRoleList(pager);
		pager.setList(list);
	}
	
	/**
	 * 根据ID查询用户角色记录
	 * @author lunli
	 * @param id
	 * @return domain
	 * @throws BusinessException
	 */
	public SysUserRole selectSysUserRoleById(Long id) throws BusinessException{
		return sysUserRoleDao.selectSysUserRoleById(id);
	}

	/**
	 *  删除用户角色记录
	 * @author lunli	 
	 * @param id
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int deleteSysUserRoleById(Long id) throws BusinessException{
		return sysUserRoleDao.deleteSysUserRoleById(id);
	}
	
	/**
	 * 修改用户角色记录
	 * @author lunli
	 * @param sysUserRole
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int updateSysUserRole(SysUserRole sysUserRole) throws BusinessException{
		return sysUserRoleDao.updateSysUserRole(sysUserRole);
	}

	/**
	 * 新增用户角色记录
	 * @author lunli
	 * @param sysUserRole
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor=BusinessException.class)
	public int createSysUserRole(SysUserRole sysUserRole) throws BusinessException{
		return sysUserRoleDao.createSysUserRole(sysUserRole);
	}

	@Override
	public  List<SysUserRole>  selectSysUserRoleList(Long userId) throws BusinessException {
		return sysUserRoleDao.searchRoleByUserId(userId);
	}

	@Override
	public int saveSysUserRole(Long userId,SysUserRole sysUserRole) throws BusinessException {
		if(sysUserRole == null){
			return -1;
		}
		
		List<SysUserRole> list = sysUserRoleService.selectSysUserRoleList(userId);
		if(sysUserRole.getRoleIds() == null || sysUserRole.getRoleIds().length == 0){ //表示前端取消了所有权限
			if(list.size() != 0){
				for(SysUserRole l :list){
					sysUserRoleService.deleteSysUserRoleById(l.getId());
				}
			}
		}else{ //选择了相关权限
			Map<Long,SysUserRole> mapSysUserRole = new HashMap<Long,SysUserRole>();
			for(SysUserRole l :list){ 
				mapSysUserRole.put(l.getSysRoleId(), l);
			}
			Set<Long> keySet = mapSysUserRole.keySet();//数据库的用户角色Id
			
			List<Long> listRoleIds = new ArrayList<Long>();
			listRoleIds = Arrays.asList(sysUserRole.getRoleIds()); //提交回来的角色ID数据
			
			SysUserRole createUserRole = null;
			for(Long li : listRoleIds){ //提交的
				if(mapSysUserRole.get(li) == null){ //数据库
					createUserRole = new SysUserRole();
					createUserRole.setSysRoleId(li);
					createUserRole.setSysUserId(userId);
					sysUserRoleService.createSysUserRole(createUserRole);
				}
			}
			for(Long l : keySet){
				if(!listRoleIds.contains(l)){
					sysUserRoleService.deleteSysUserRoleById(mapSysUserRole.get(l).getId());
				}
			}
			return 1;
		}
		return 0;
	}
	

	

}
